﻿Imports TMySQL
Public Class frmTable
    Implements ITabForm
    Private _frmTableSource As frmTableSource
    Private _frmTableDef As frmTableDef
    Private _frmIndex As frmIndex
    Private _frmTriggers As frmTriggers
    Private _frmTableStatus As frmTableStatus
    Private _frmPrefixIndex As frmPrefixIndex

    Private _Table As IHasColumns
    Public Sub New(ByVal ATable As IHasColumns)
        Me.New()
        _Table = ATable
        InitFrm()
    End Sub
    Private Sub New()
        ' この呼び出しは、Windows フォーム デザイナで必要です。
        InitializeComponent()
        ' InitializeComponent() 呼び出しの後で初期化を追加します。
    End Sub
    Public ReadOnly Property Table() As IHasColumns
        Get
            Return _Table
        End Get
    End Property
    Private Sub InitFrm()
        InitTable()
        InitIndex()
        InitTrigger()
        InitStatus()
        InitSource()
        InitPrefixIndex()
        frmMain.GeneralSettings.ApplyAll(Me)
        frmMain.GridSettings.ApplyAll(Me)
    End Sub
    Private Sub InitPrefixIndex()
        _frmPrefixIndex = New frmPrefixIndex(_Table)
        _frmPrefixIndex.Dock = DockStyle.Fill
        _frmPrefixIndex.TopLevel = False
        _frmPrefixIndex.Parent = tbsPrefixIndex
        _frmPrefixIndex.Show()
    End Sub
    Private Sub InitSource()
        _frmTableSource = New frmTableSource(_Table)
        _frmTableSource.Dock = DockStyle.Fill
        _frmTableSource.TopLevel = False
        _frmTableSource.Parent = tbsDef
        _frmTableSource.Show()
    End Sub
    Private Sub InitTable()
        _frmTableDef = New frmTableDef(_Table)
        _frmTableDef.Dock = DockStyle.Fill
        _frmTableDef.TopLevel = False
        _frmTableDef.Parent = tbsTable
        _frmTableDef.Show()
    End Sub
    Private Sub InitIndex()
        _frmIndex = New frmIndex(_Table)
        _frmIndex.Dock = DockStyle.Fill
        _frmIndex.TopLevel = False
        _frmIndex.Parent = tbsIndex
        _frmIndex.Show()
    End Sub
    Private Sub InitTrigger()
        _frmTriggers = New frmTriggers(_Table)
        _frmTriggers.Dock = DockStyle.Fill
        _frmTriggers.TopLevel = False
        _frmTriggers.Parent = tbsTrigger
        _frmTriggers.Show()
    End Sub
    Private Sub InitStatus()
        _frmTableStatus = New frmTableStatus(_Table)
        _frmTableStatus.Dock = DockStyle.Fill
        _frmTableStatus.TopLevel = False
        _frmTableStatus.Parent = tbsStatus
        _frmTableStatus.Show()
    End Sub

    Protected Overrides Sub Finalize()
        If Not _Table Is Nothing Then
            _Table = Nothing
        End If
        MyBase.Finalize()
    End Sub

    Private Sub cmdShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdShow.Click
        Cursor.Current = Cursors.WaitCursor
        Try
            _frmTableDef.RefreshData()
        Finally
            Cursor.Current = Cursors.Default
        End Try
    End Sub

    Private Sub cmdSaveData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSaveData.Click
        If SaveDlg.ShowDialog = Windows.Forms.DialogResult.OK Then
            Cursor.Current = Cursors.WaitCursor
            Try
                _Table.RefreshData()
                _Table.SaveData(SaveDlg.FileName)
                Call MsgBox("保存しました", MsgBoxStyle.Information)
            Finally
                Cursor.Current = Cursors.Default
            End Try
        End If
    End Sub

    Private Sub cmdCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCopy.Click
        Cursor.Current = Cursors.WaitCursor
        Try
            _Table.RefreshData()
            _Table.CopyData()
            Call MsgBox("コピーしました", MsgBoxStyle.Information)
        Finally
            Cursor.Current = Cursors.Default
        End Try
    End Sub

    Private Sub cmdRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRefresh.Click
        If _frmTableDef.Edited Then
            If MsgBox("テーブルが変更されています。更新しますか？", MsgBoxStyle.Question Or MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                Try
                    AlterTable()
                    Call MsgBox("更新しました。", MsgBoxStyle.Information)
                Catch ex As Exception
                    Call MsgBox("エラーが発生しました。" & vbNewLine & ex.Message, MsgBoxStyle.Exclamation)
                    _frmTableDef.RefreshData()
                End Try
            End If
        End If
    End Sub

    Private Sub AlterTable()
        Cursor.Current = Cursors.WaitCursor
        Try
            _frmTableDef.AlterTable()
        Catch ex As Exception
            Throw New Exception(ex.Message)
        Finally
            Cursor.Current = Cursors.Default
        End Try
    End Sub
End Class